<cfcomponent>


<cffunction name="createUDF" output="false">
  <cfargument name="udfstring">
  <cfset var objname = "newmethod_#createUUID()#" />
  <cfset var filename = "#getDirectoryFromPath(getMetaData(this).path)##objname#.cfc" />
  <cfset var obj = ""/>
  <cfset var func = ""/>
  <cfset var md = ""/>
  <cfset var output= ""/>
  <cfif IsCustomFunction(arguments.udfString)>
    <cfreturn udfstring />
  </cfif>

  <cfset arguments.udfstring = trim(udfstring) />
  <cfif left(arguments.udfstring,8) EQ "function">
    <!--- it's a cfscript function --->
    <cfset output="<cfcomponent><cfscript>#arguments.udfString#</cfscript></cfcomponent>" />
  <cfelseif left(arguments.udfstring,11) EQ "<cffunction">
    <!--- it's a tag function --->
    <cfset output="<cfcomponent>#arguments.udfString#</cfcomponent>" />
  <cfelse>
    <!--- it's not a tag or script function.  See if we can create one assuming cfscript snippet was passed in --->
    <cfset output="
      <cfcomponent>
        <cffunction name='f' output='false'>
          <cfargument name='item' required='true'>
          <cfargument name='scope' required='true'>
          <cfargument name='index' required='true'>
          <cfargument name='length' required='true'>
          <cfscript>
            var _l=arguments.scope;
            var _ix=arguments.index;
            var _it= arguments.item;
            var _len=arguments.length;
            #arguments.udfString#
          </cfscript>
        </cffunction>
      </cfcomponent>" />
  </cfif>
  <cftry>

    <cffile
      action="write"
      output="#output#"
      file="#filename#" />
    <cfcatch>
      <!--- could not create file - system error --->
      <cfset deleteComponent(filename) />
      <cfthrow message="System Error.  Could not create UDF (1)." detail="Could not create file for udf: #arguments.udfString# - #cfcatch.message# - #cfcatch.detail#"
                type="#getMetaData(this).name#.createUDF.create_file_error">
    </cfcatch>

  </cftry>

  <cftry>
    <cfset obj = createObject("component",objname) />
    <cfset md = getMetadata(obj) />
    <cfif structKeyExists(md,"functions") AND arrayLen(md.functions) EQ 1 >
      <cfset func = obj[md.functions[1].name] />
    <cfelse>
      <cfthrow message="method was not defined" detail="no method was defined in generated component createUDF()" >
    </cfif>

    <cfcatch>
      <!--- could not create file - system error --->
      <cfset deleteComponent(filename) />
      <cfthrow message="System Error.  Could not create UDF (2)." detail="Could not get instance of UDF for CreateUDF(): #arguments.udfString# - #cfcatch.message# - #cfcatch.detail#"
                type="#getMetaData(this).name#.createUDF.createObject_error">
    </cfcatch>

  </cftry>


  <cfset deleteComponent(filename) />
  <cfreturn func />
</cffunction>

<cffunction name="deleteComponent" output="false" access="private">
  <cfargument name="filename">

  <cftry>
    <cffile
      action="delete"
      file="#arguments.filename#" />
    <cfcatch>
      <!--- just ignore if we can't delete the file --->
    </cfcatch>
  </cftry>
</cffunction>
</cfcomponent>
